/**
 * Created by sxwu on 14-2-15.
 */


var log = require("../lib/log");
var config = require('../config');
var virwifiinfo = require('../models/virwifiinfo');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');

exports.index = function(request, response){
    request.session.lastpage="setwifi.html";
    if(request.session.admin==undefined)
    {
        response.redirect("login.html");
    }
    else
    {

        var method = request.method || '';
        var data = RUtil.wifi_data(request);
        data.front_data.title = '基本配置';
        data.front_data.subtitle='WiFi热点配置';
        data.front_data.usernick =request.session.admin.usernick;
        data.front_data.usertype =request.session.admin.usertype;
        //查找virwifiinfo表看运营商是否有配置wifi信息，有则获取并推送到页面上，没有则显示空

        if(method.toUpperCase() === "GET") {
            virwifiinfo.select(request.session.admin.userid, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    data.front_data.errDesp = "查询失败：系统内部错误(错误码："+error.errno+")";
                    data.front_data.status="danger";
                    //response.render("setwifi", data);
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    data.front_data.errDesp = "您还没有配置任何WiFi信息，请先配置如下信息：";
                    data.front_data.status="success";
                    //根据商户的id生成唯一的微信code
                    data.wifiinfo.weixincode="点击<a href=\""+"useronlineinfo.html"+"?autologin&wifitoken="+
                    crypto.createHash('md5').update(request.session.admin.userid.toString()).digest('hex')+"_"+request.session.admin.userid+
                    "\">我要上网</a>";

                    //response.render("setwifi", data);
                } else {
                    //存在数据将数据发到页面
                    var tmpwifiinfo = results[0];
                    data.wifiinfo.wifissid = tmpwifiinfo.wifissid;
                    data.wifiinfo.wifiauthtype= tmpwifiinfo.wifiauthtype;
                    data.wifiinfo.wifiauthhour=tmpwifiinfo.wifiauthhour;
                    data.wifiinfo.wifiauthbrand= tmpwifiinfo.wifiauthbrand;
                    data.wifiinfo.wifipassdomain= tmpwifiinfo.wifipassdomain;

                    if(tmpwifiinfo.wifiredirecturl==null)
                    {
                        data.wifiinfo.wifiredirecturl="useronlineinfo.html";
                    }
                    else
                    {
                        data.wifiinfo.wifiredirecturl=tmpwifiinfo.wifiredirecturl;
                    }

                    data.wifiinfo.weixincode="点击<a href=\""+data.wifiinfo.wifiredirecturl+"?autologin&wifitoken="+
                        crypto.createHash('md5').update(request.session.admin.userid.toString()).digest('hex')+"_"+request.session.admin.userid+
                        "\">我要上网</a>";


                }
                response.render("setwifi", data);
            });
        }

        else if(method.toUpperCase() === "POST") {
            data.wifiinfo.wifiuserid = request.session.admin.userid;
            data.wifiinfo.wifissid = request.body.wifissid;
            if(typeof(request.body.wifiauthtype)!=="undefined" && typeof(request.body.wifiauthtype)!=="string")
            {
                data.wifiinfo.wifiauthtype = eval(request.body.wifiauthtype.join("+"));
            }
            else if(request.body.wifiauthtype==null)
            {
                data.wifiinfo.wifiauthtype = 2;
            }
            else
            {
                data.wifiinfo.wifiauthtype = request.body.wifiauthtype;
            }
            if(typeof(request.body.wifiredirecturl)!=="undefined")
            {
                data.wifiinfo.wifiredirecturl=request.body.wifiredirecturl;
            }
            data.wifiinfo.wifiauthhour = request.body.wifiauthhour;
            data.wifiinfo.wifiauthbrand = request.body.wifiauthbrand;
            data.wifiinfo.wifipassdomain= request.body.wifipassdomain;

            data.wifiinfo.weixincode="点击<a href=\""+data.wifiinfo.wifiredirecturl+"?autologin&wifitoken="+
                crypto.createHash('md5').update(request.session.admin.userid.toString()).digest('hex')+"_"+request.session.admin.userid+
                "\">我要上网</a>";
            virwifiinfo.select(request.session.admin.userid, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    data.front_data.errDesp = "查询失败：系统内部错误(错误码："+error.errno+")";
                    data.front_data.status="danger";
                    //response.render("setwifi", data);
                }
                else if(results.length <= 0) {
                    // 不存在,直接插入数据
                    virwifiinfo.insert(data.wifiinfo, function(error,results) {
                        if((error)!== null)
                        {
                            // 数据库异常
                            data.front_data.errDesp = "设置失败：系统内部错误(错误码："+error.errno+")";
                            data.front_data.status="danger";
                        }
                        else if(results.length <= 0) {
                            data.front_data.errDesp = "设置失败：系统内部错误(错误码："+error.errno+")";
                            data.front_data.status="danger";
                        } else {
                            data.front_data.errDesp="WiFi热点信息设置成功！";
                            data.front_data.status="success";
                        }
                        response.render("setwifi", data);
                    });
                } else {
                    //存在数据，直接更新数据
                    virwifiinfo.update(data.wifiinfo, function(error,results) {
                        if((error)!== null)
                        {
                            // 数据库异常
                            data.front_data.errDesp = "设置失败：系统内部错误(错误码："+error.errno+")";
                            data.front_data.status="danger";
                        }
                        else if(results.length <= 0) {
                            data.front_data.errDesp = "设置失败：系统内部错误(错误码："+error.errno+")";
                            data.front_data.status="danger";
                        } else {
                            data.front_data.errDesp="WiFi热点信息设置成功！";
                            data.front_data.status="success";
                        }
                        response.render("setwifi", data);
                    });
                }
            });


        }
    }
};
